/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.factories;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.tbjd.client.beans.Project;
import org.tbjd.client.beans.Resource;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.TimeConstants;

import com.google.gwt.user.client.Random;

public class TaskFactory {

	private static int position = 0;
	static private List<Task> tasks = new ArrayList<Task>();

	public static List<Task> getInstances(int n) {
		Task task = null;
		for (int i = 0; i < n; i++) {
			int nbChildren = Random.nextInt(4);

			task = new Task();
			task.setId(Random.nextInt(50));
			task.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(5))));
			task.setLevel(position);

			if (nbChildren > 0) {
				task.setName("Phase " + i);
				task.setType(Task.PHASE);

			} else {
				task.setName("Tache " + i);
				task.setType(Task.TASK);
				task.setDuration(1 + Random.nextInt(96));
				task.setProgress(1 + Random.nextInt(100));
			}

			for (int j = 0; j < nbChildren; j++) {
				Task childTask = new Task();
				childTask.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * position++)));
				childTask.setId(Random.nextInt(50));
				childTask.setType(Task.TASK);
				childTask.setName("Tache enfant" + i + ":" + j);
				childTask.setProgress(1 + Random.nextInt(100));
				childTask.setLevel(position++);
				childTask.setDuration(1 + Random.nextInt(96));
				childTask
						.setEndDate(new Date((long) (childTask.getBeginDate().getTime() + (childTask.getDuration() / TimeConstants.HOUR_IN_MILLIS))));
				childTask.setParent(task);

			}
			tasks.add(task);
			position++;
		}
		// display();
		return tasks;
	}

	public static List<Task> getInstancesWithResources(int n) {
		List<Task> tasks = getInstances(n);
		List<Resource> resources = new ArrayList<Resource>();

		for (Task task : tasks) {
			// empty the list of resources
			resources.clear();

			// define a random list of resources
			for (int i = 0; i < Random.nextInt(3); i++) {

				// add the to the task
				task.addResource(ResourceFactory.getInstance());

			}

		}

		return tasks;
	}

	public static List<Task> getFixedTasksWithResources(Project project) {
		List<Task> tasks = new ArrayList<Task>();

		Task t1 = new Task(project, Task.TASK);
		t1.setId(0);
		t1.setName("t1");
		t1.setLevel(0);
		t1.setProgress(30);
		t1.setEndDate(new Date(t1.getEndDate().getTime() + TimeConstants.DAY_IN_MILLIS));
		t1.setDuration(48f);
		t1.setEffort(48f);
		tasks.add(t1);

		Task t2 = new Task(project, Task.TASK);
		t2.setId(1);
		t2.setName("t2");
		t2.setLevel(2);

		Task t3 = new Task(project, Task.TASK);
		t3.setId(2);
		t3.setName("t3");
		t3.setLevel(3);
		t3.setBeginDate(new Date(t3.getBeginDate().getTime() + TimeConstants.DAY_IN_MILLIS * 3));
		t3.setEndDate(new Date(t3.getBeginDate().getTime() + TimeConstants.DAY_IN_MILLIS));

		Task t4 = new Task(project, Task.TASK);
		t4.setId(3);
		t4.setName("t4");
		t4.setLevel(4);
		tasks.add(t4);

		Task t5 = new Task(project, Task.TASK);
		t5.setId(4);
		t5.setName("t5");
		t5.setLevel(5);
		tasks.add(t5);

		Task t6 = new Task(project, Task.TASK);
		t6.setId(5);
		t6.setName("t6");
		t6.setLevel(6);
		t6.setResource(ResourceFactory.getInstance());
		tasks.add(t6);

		Task t7 = new Task(project, Task.TASK);
		t7.setId(6);
		t7.setName("t7");
		t7.setLevel(7);
		tasks.add(t7);

		Task f1 = new Task(project, Task.PHASE);
		f1.setId(100);
		f1.setName("phase 1");
		f1.setLevel(1);
		f1.addChild(t2);
		f1.addChild(t3);
		tasks.add(f1);

		return tasks;
	}
	
	static void display() {
		for (Task eachTask : tasks) {
			System.out.println("id:" + eachTask.getId() + " - level:" + eachTask.getLevel() + " - type:" + eachTask.getType() + " - child:"
					+ eachTask.getLstChildren().size());
			for (Task eachChild : eachTask.getLstChildren()) {
				System.out.println("---------> id:" + eachChild.getId() + " - level:" + eachChild.getLevel() + " - duration:"
						+ eachChild.getDuration() + " - type:" + eachChild.getType() + " - child:" + eachChild.getLstChildren().size());
			}
		}
	}
}
